package com.bootdo.wx.service.impl;

import com.bootdo.wx.dao.CmsContentDao;
import com.bootdo.wx.dao.GoodsGoodsDao;
import com.bootdo.wx.dao.GoodsProductDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.CmsContentDO;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.GoodsProductDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.GoodsProductService;
import com.bootdo.wx.service.GoodsSpikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class GoodsProductServiceImpl implements GoodsProductService {
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private GoodsSpikeService goodsSpikeService;
	@Autowired
	private CmsContentDao cmsContentDao;
	
	
	@Override
	public GoodsProductDO get(String id){
		return goodsProductDao.get(id);
	}
	
	@Override
	public List<GoodsProductDO> list(Map<String, Object> map){
		return goodsProductDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsProductDao.count(map);
	}
	
	@Override
	public int save(GoodsProductDO goodsProduct){
		return goodsProductDao.save(goodsProduct);
	}
	
	@Override
	public int update(GoodsProductDO goodsProduct){
		return goodsProductDao.update(goodsProduct);
	}
	
	@Override
	public int remove(String id){
		return goodsProductDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsProductDao.batchRemove(ids);
	}
	
	/**
	 * 生成货号前缀的属性
	 */
	private static final char[] _UU32 = "0123456789abcdefghijklmnopqrstuv".toCharArray();
	
	/**
	 * 生成货号前缀的方法
	 * @param uu
	 * @return
	 */
	public static String UU32(UUID uu) {
		StringBuilder sb = new StringBuilder();
		long m = uu.getMostSignificantBits();
		long l = uu.getLeastSignificantBits();
		int i;
		for(i = 0; i < 13; ++i) {
			sb.append(_UU32[(int)(m >> (13 - i - 1) * 5) & 31]);
		}
		for(i = 0; i < 13; ++i) {
			sb.append(_UU32[(int)(l >> (13 - i - 1) * 5) & 31]);
		}
		return sb.toString();
	}

	@Override
	public String getSkuPrefix() {
		String uuid = "S" + UU32(UUID.randomUUID()).substring(1, 12).toUpperCase();
		List<GoodsProductDO> list = goodsProductDao.queryBySku(uuid);
		if (list!=null && list.size()>0){
			return getSkuPrefix();
		}
		return uuid;
	}

	/*真删除*/
	@Override
	public int removeByGoodsId(String goodsId) {
		return goodsProductDao.removeByGoodsId(goodsId);
	}

	/*假删除*/
	@Override
	public int removeByGoodsIdJ(String goodsId) {
		return goodsProductDao.removeByGoodsIdJ(goodsId);
	}

	@Override
	public Map<String,Object> getByGoodsIdAndSpec(Map<String, Object> params) {
		String type = params.get("type").toString();
		String userId = params.get("userId").toString();
		MemberUserDO userDO = memberUserDao.get(userId);
		Map<String,Object> productMap = new HashMap<String,Object>();
		if ("0".equals(type)) {
			productMap =goodsProductDao.getByGoodsIdAndSpec(params);
		} else if ("1".equals(type)) {
			productMap =goodsProductDao.getBySpikeIdAndSpec(params);
		} else if ("2".equals(type)) {
			productMap =goodsProductDao.getByNineIdAndSpec(params);
		} else if ("4".equals(type)) {
			productMap =goodsProductDao.getByTeamIdAndSpec(params);
		} else if ("5".equals(type)) {
			productMap =goodsProductDao.getByUpgradeIdAndSpec(params);
		}
		GoodsGoodsDO goodsGoodsDO =goodsGoodsDao.get(productMap.get("goods_id").toString());
		int isvip = goodsGoodsDO.getIsvip();
		int level = userDO.getLevel();
		int postSize = goodsGoodsDO.getPostSize();  //运费模板类型  0：自定义运费，1：按重量模板
		BigDecimal weight = new BigDecimal(productMap.get("weight").toString()).divide(new BigDecimal(1000));
		BigDecimal postFee = new BigDecimal(productMap.get("post_fee").toString());
		if (isvip == 0) {
			productMap.put("minePrice",productMap.get("price"));
		} else {
			switch (level) {
				case 1 :
					productMap.put("minePrice", productMap.get("price_c"));
					break;
				case 2 :
					productMap.put("minePrice", productMap.get("price_z"));
					break;
				case 3 :
					productMap.put("minePrice", productMap.get("price_g"));
					break;
				default:
					productMap.put("minePrice",productMap.get("price"));
			}
		}
		if ("5".equals(type)) {
			productMap.put("minePrice",productMap.get("price"));
		}
		if (postSize == 0) {
			productMap.put("post_fee",postFee);
		} else {
			CmsContentDO contentDO = cmsContentDao.getByType("post-weight").get(0);
			BigDecimal value = new BigDecimal(contentDO.getValue());
			postFee = value.multiply(weight);
			productMap.put("post_fee",postFee);
		}
		return productMap;
	}
	
}
